//
//  AroundPeopleViewControllers.m
//  穷游项目
//
//  Created by 林程杰 on 14/12/30.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//
#import "macro.h"
#import "AroundPeopleViewControllers.h"
#import "WebViewController.h"
#import "Masonry.h"
#import "WebColor.h"
#import "AFJSONRequestOperation.h"

@implementation AroundPeopleViewControllers


- (void)dealloc
{
    [_dicView release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.dicView = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void) viewDidLoad
{
    [super viewDidLoad];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    imageView.image = [UIImage imageNamed:@"mohu.jpg"];
    [self.view addSubview:imageView];
    [imageView release];
    [self json];
    
}

- (void) json
{
    NSString *str = @"http://open.qyer.com/qyer/chatroom/stats?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&v=1&track_user_id=&track_deviceid=08A49846-60B9-49CE-9C44-271EA6ABA389&track_app_version=5.4.4&track_app_channel=App%20Store&track_device_info=iPhone%205s&track_os=ios%208.1.2&lat=38.882988&lon=121.539856&app_installtime=1419660241&hot=3&new=3";
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSDictionary *dicData = [JSON objectForKey:@"data"];
        [_dicView removeAllObjects];
        [_dicView setValue:dicData forKey:@"dicData"];
        [self creatView];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"error == %@", error);
    }];
    [operation start];
}



- (void) creatView
{
    UIView *view = [[UIView alloc] init];
    [view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(super.view.mas_left).offset(15);
        make.right.equalTo(super.view.mas_right).offset(-15);
        make.top.equalTo(super.view.mas_top).offset(70);
        make.bottom.equalTo(super.view.mas_bottom).offset(-70);
    }];
    
    
    UILabel *labelTitleLeft = [[UILabel alloc] init];
    [view addSubview:labelTitleLeft];
    
    [labelTitleLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(35);
        make.top.equalTo(super.view.mas_top).offset(90);
        make.right.equalTo(view.mas_right).offset(-130);
    }];
    
    UILabel *labelTitleRight = [[UILabel alloc] init];
    [view addSubview:labelTitleRight];
    
    [labelTitleRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(labelTitleLeft.mas_right).offset(5);
        make.top.equalTo(super.view.mas_top).offset(90);
        make.right.equalTo(view.mas_right).offset(-35);
    }];
    
    
    
    UILabel *labelMessage = [[UILabel alloc] init];
    labelMessage.numberOfLines = 0;
    [view addSubview:labelMessage];
    
    [labelMessage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(10);
        make.top.equalTo(view.mas_top).offset(60);
        make.right.equalTo(view.mas_right).offset(-20);
    }];
    labelMessage.numberOfLines = 0;
    
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"flag.png"];
    [view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(10);
        make.top.equalTo(labelMessage.mas_bottom).offset(30);
        
        make.bottom.equalTo(view.mas_bottom).offset(-190);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    UILabel *labelCity = [[UILabel alloc] init];
    [view addSubview:labelCity];
    [labelCity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(10);
        make.top.equalTo(labelMessage.mas_bottom).offset(28);
        make.right.equalTo(view.mas_right).offset(-160);
        make.bottom.equalTo(view.mas_bottom).offset(-190);
    }];
    
    //城市列表按钮
    UIButton *buttonCity = [[UIButton alloc] init];
    [buttonCity setTitle:@"城市列表>" forState:UIControlStateNormal];
    [buttonCity setBackgroundImage:[UIImage imageNamed:@"gray.png"] forState:UIControlStateNormal];
    buttonCity.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [buttonCity addTarget:self action:@selector(createCityView:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:buttonCity];
    
    [buttonCity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(labelCity.mas_right).offset(10);
        make.top.equalTo(labelMessage.mas_bottom).offset(25);
        make.right.equalTo(view.mas_right).offset(-20);
        make.bottom.equalTo(view.mas_bottom).offset(-190);
        
    }];
    
    
    UILabel *labelBridge = [[UILabel alloc] init];
    [view addSubview:labelBridge];
    [labelBridge mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(50);
        make.top.equalTo(labelCity.mas_bottom).offset(27);
        make.right.equalTo(view.mas_right).offset(-90);
        
    }];
    
    UILabel *labelHK = [[UILabel alloc] init];
    [view addSubview:labelHK];
    [labelHK mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(50);
        make.top.equalTo(labelBridge.mas_bottom).offset(5);
        make.right.equalTo(view.mas_right).offset(-90);
        
    }];
    
    
    UILabel *labelParis = [[UILabel alloc] initWithFrame:CGRectMake(50, 315, 110, 20)];
    [view addSubview:labelParis];
    [labelParis mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(50);
        make.top.equalTo(labelHK.mas_bottom).offset(5);
        make.right.equalTo(view.mas_right).offset(-90);
        
    }];
    
    
    UILabel *labelBridge_Count = [[UILabel alloc] init];
    [view addSubview:labelBridge_Count];
    [labelBridge_Count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(180);
        make.top.equalTo(labelCity.mas_bottom).offset(27);
        make.right.equalTo(view.mas_right).offset(-30);
        
    }];
    
    UILabel *labelHK_Count = [[UILabel alloc] init];
    [view addSubview:labelHK_Count];
    [labelHK_Count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(180);
        make.top.equalTo(labelBridge_Count.mas_bottom).offset(3);
        make.right.equalTo(view.mas_right).offset(-30);
        
    }];
    
    UILabel *labelParis_Count = [[UILabel alloc] init];
    
    [view addSubview:labelParis_Count];
    [labelParis_Count mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(180);
        make.top.equalTo(labelHK_Count.mas_bottom).offset(3);
        make.right.equalTo(view.mas_right).offset(-30);
        
    }];
    
    
    
    UIButton *buttonJoin = [[UIButton alloc] init];
    [view addSubview:buttonJoin];
    [buttonJoin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(20);
        make.top.equalTo(labelParis.mas_bottom).offset(30);
        make.right.equalTo(view.mas_right).offset(-10);
    }];
    //"X" Button
    
    UIButton *buttonX = [[UIButton alloc] init];
    buttonX.layer.cornerRadius = 18;
    buttonX.layer.masksToBounds = YES;
    [view addSubview:buttonX];
    
    [buttonX mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_top).offset(-18);
        make.right.equalTo(view.mas_right).offset(10);
        make.size.mas_equalTo(CGSizeMake(37, 37));
    }];
    
    UIImageView *images = [[UIImageView alloc] initWithFrame:CGRectMake(6, 6, 25, 25)];
    [buttonX addSubview:images];
    images.image = [UIImage imageNamed:@"close.png"];
    [buttonX setBackgroundColor:[WebColor whiteSmoke]];
    [buttonX addTarget:self action:@selector(returnPage :) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    //赋值
    NSDictionary *dicData = [_dicView objectForKey:@"dicData"];
    
    NSArray *arrayHot = [dicData objectForKey:@"hot"];
    NSDictionary *dic0 = [arrayHot objectAtIndex:0];
    NSDictionary *dic1 = [arrayHot objectAtIndex:1];
    NSDictionary *dic2 = [arrayHot objectAtIndex:2];
    NSNumber *bridge = [dic0 objectForKey:@"members"];
    NSNumber *HK = [dic1 objectForKey:@"members"];
    NSNumber *paris = [dic2 objectForKey:@"members"];
    
    [labelTitleLeft setText:@"寻找身边"];
    labelTitleLeft.font = [UIFont systemFontOfSize:30];
    
    [labelTitleRight setText:@"基游"];
    labelTitleRight.font = [UIFont systemFontOfSize:30];
    labelTitleRight.textColor = [WebColor seaGreen];
    
    labelMessage.text = @"身边人这项功能目前已经覆盖了海外150个城市, 包括曼谷, 吉隆坡, 巴黎, 香港... \n\n它让每一段旅行,  都不再孤单。同城组爬梯, 异国有盆友。吃饭聊天看风景, 求助问询查信息。旅途中, 在这里发现身边的同路人。";
    labelMessage.font = [UIFont systemFontOfSize:15];
    labelMessage.textColor = [UIColor grayColor];
    
    [labelCity setText:@"当前热门城市"];
    labelCity.font = [UIFont systemFontOfSize:15];
    labelCity.textColor = [UIColor grayColor];
    
    
    [labelBridge setText:@"剑桥身边人在线: "];
    labelBridge.font = [UIFont systemFontOfSize:15];
    [labelHK setText:@"香港身边人在线: "];
    labelHK.font = [UIFont systemFontOfSize:15];
    [labelParis setText:@"巴黎身边人在线: "];
    labelParis.font = [UIFont systemFontOfSize:15];
    
    labelBridge_Count.text = [NSString stringWithFormat:@"%@ 人", bridge];
    labelBridge_Count.textColor = [WebColor seaGreen];
    labelHK_Count.text = [NSString stringWithFormat:@"%@ 人", HK];
    labelHK_Count.textColor = [WebColor seaGreen];
    labelParis_Count.text = [NSString stringWithFormat:@"%@ 人", paris];
    labelParis_Count.textColor = [WebColor seaGreen];
    
    [buttonJoin setTitle:@"我在海外, 立即加入身边人 !" forState:UIControlStateNormal];
    buttonJoin.titleLabel.font = [UIFont systemFontOfSize:20];
    [buttonJoin setBackgroundColor:[WebColor seaGreen]];
    [buttonJoin addTarget:self action:@selector(myLogin2:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [labelBridge release];
    [labelBridge_Count release];
    [labelHK release];//
    [labelHK_Count release];
    [labelMessage release];
    [labelParis release];
    [labelParis_Count release];
    [labelTitleLeft release];
    [labelTitleRight release];
    [buttonJoin release];
    [view release];
    [buttonX release];
    [imageView release];
    [images release];
    [labelCity release];
    [buttonCity release];
}



- (void) createCityView: (id) sender
{
    
    NSDictionary *dicData = [_dicView objectForKey:@"dicData"];
    NSString *myWebView = [dicData objectForKey:@"bbs_link"];//html
    
    
    WebViewController *web = [[WebViewController alloc] init];
    web.html = myWebView;
    
    UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:web];
    na.navigationBar.barTintColor = [WebColor mediumSeaGreen];
    [self presentViewController:na animated:YES completion:nil];
    [web release];
    [na release];
    
}


- (void) returnPage : (UIButton *) button
{
    [self dismissViewControllerAnimated:NO completion:nil];
    
}

- (void) myLogin2 : (id) sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"当前城市暂未开通身边人功能" message:nil delegate:self cancelButtonTitle:@"查看城市列表" otherButtonTitles:nil, nil];
    alert.delegate = self;
    [alert show];
    [alert release];
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSDictionary *dicData = [_dicView objectForKey:@"dicData"];
        NSString *myWebView = [dicData objectForKey:@"bbs_link"];//html
        
        
        WebViewController *web = [[WebViewController alloc] init];
        web.html = myWebView;
        
        UINavigationController *na = [[UINavigationController alloc] initWithRootViewController:web];
        na.navigationBar.barTintColor = [WebColor mediumSeaGreen];
        [self presentViewController:na animated:YES completion:nil];
        [web release];
        [na release];
    }
    
}


@end
